package com.gwkj.zxjl_user;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;

import com.gwkj.zxjl_user.AccountDB.ADBService;
import com.gwkj.zxjl_user.Bean.Other.DeviceEntity;
import com.gwkj.zxjl_user.Utils.DeviceUtils;
import com.gwkj.zxjl_user.Widget.GalleryFinal.UILImageLoader;
import com.gwkj.zxjl_user.Widget.GalleryFinal.UILPauseOnScrollListener;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.io.File;

import cn.finalteam.galleryfinal.CoreConfig;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.ThemeConfig;

/**
 * Created by fujh on 15/7/29.
 */
public class UILApplication extends Application{

    @Override
    public void onCreate() {
        super.onCreate();

        initGalleryFinal(getApplicationContext());
        initImageLoader(getApplicationContext());
        initAppInfo(getApplicationContext());
    }

    /**
     * 相册
     * @param context
     */
    private void initGalleryFinal(Context context){
        ThemeConfig theme = ThemeConfig.CYAN;

        FunctionConfig functionConfig = new FunctionConfig.Builder()
                .setEnableCamera(true)
                .setEnableEdit(false)
                .setEnableCrop(true)
                .setEnableRotate(false)
                .setCropSquare(true)
                .setEnablePreview(false)
                .build();

        CoreConfig coreConfig = new CoreConfig.Builder(context, new UILImageLoader(), theme)
                .setFunctionConfig(functionConfig)
                .setPauseOnScrollListener(new UILPauseOnScrollListener(false, true))
                .build();

        GalleryFinal.init(coreConfig);
    }

    /**
     * 图像
     * @param context
     */
    private void initImageLoader(Context context) {

        File cacheDir = new File(Constants.PATH.IMG_PATH);
        if (!cacheDir.exists()) {
            cacheDir.mkdirs();
        }

        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(false)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .diskCache(new UnlimitedDiskCache(cacheDir))
                .diskCacheFileCount(200)
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .defaultDisplayImageOptions(options)
                .writeDebugLogs()
                .build();

        ImageLoader.getInstance().init(config);
    }

    /**
     * 初始化设备信息  并存入数据库
     */
    private void initAppInfo(Context context) {
        DeviceEntity device = new DeviceEntity();
        device.setPlatform(DeviceUtils.getPlatform());
        device.setVersion(DeviceUtils.getVersionName(context));
        device.setApp(DeviceUtils.getApp());
        device.setDevice_id(DeviceUtils.getDevice_id(context));
        device.setDevice_info(DeviceUtils.getDevice_info());
        ADBService adbService = new ADBService(context);
        adbService.updateDevice(device);
    }
}
